package LeetCode._01_树;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class _257_二叉树的所有路径 {
    public class TreeNode {
        int val;
       TreeNode left;
       TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val,TreeNode left,TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    //记录递归时的路径
    LinkedList<String> path = new LinkedList<>();
    //记录最终结果
    LinkedList<String> res = new LinkedList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        traver(root);
        return res;
    }

   void traver(TreeNode root){
        if(root == null) return;

        if(root.left == null && root.right == null){
            path.addLast(root.val+"");
            res.addLast(String.join("->",path));
            path.removeLast();
            return;
        }

        path.addLast(root.val+"");
        traver(root.left);
        traver(root.right);
        path.removeLast();
   }

}
